package ch.zbw.pra.sysventory.presentation.panels;

import java.util.Observable;
import java.util.Observer;

import ch.zbw.pra.sysventory.model.Scanversion;
import ch.zbw.pra.sysventory.presentation.main.Main;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

/**
 * Painted das ApplicationPanel
 * 
 * @author Ralph Koster
 */

public class ApplicationPanel extends CssLayout implements Observer {

	private static final long serialVersionUID = -7855978453758118918L;
	private Main main;
	private HorizontalLayout mainPanel;
	private VerticalLayout content;
	private NavigationPanel navigationPanel;
	private HomePanel homePanel;
	private ScanPanel scanPanel;
	private ArchivePanel archivePanel;
	private StatisticsPanel statisticsPanel;
	private SettingsPanel settingsPanel;
	
	public ApplicationPanel(Main main) {
		this.main = main;
		this.mainPanel = new HorizontalLayout();
        this.content = new VerticalLayout();
        this.navigationPanel = new NavigationPanel(this);
        this.homePanel = new HomePanel();
        this.scanPanel = new ScanPanel(this);
        this.archivePanel = new ArchivePanel();
        this.statisticsPanel = new StatisticsPanel();
        this.settingsPanel = new SettingsPanel();
        
		paint();
	}
	
	public void paint() {
		content.addComponent(homePanel);
		content.setSizeUndefined();
		content.setMargin(false);
		content.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		content.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		content.setStyleName("application-panel-content");
        
		mainPanel.setSizeFull();
		mainPanel.setMargin(false);
        mainPanel.addComponentAsFirst(navigationPanel);
        mainPanel.addComponent(content);
        mainPanel.setExpandRatio(navigationPanel, 1);
        mainPanel.setExpandRatio(content, 100);
        
        this.setSizeFull();
        this.setMargin(false);
        this.setStyleName("application-panel");
        this.addComponent(mainPanel);
	}
	
	public void changeView(String viewName) {
		if("button-home".equals(viewName)){
			content.removeAllComponents();
			content.addComponent(homePanel);
		} else if("button-scan".equals(viewName)) {
			content.removeAllComponents();
			content.addComponent(scanPanel);
		} else if("button-archiv".equals(viewName)) {
			content.removeAllComponents();
			content.addComponent(archivePanel);
		} else if("button-statistik".equals(viewName)) {
			content.removeAllComponents();
			content.addComponent(statisticsPanel);
		} else {
			content.removeAllComponents();
			content.addComponent(settingsPanel);
		}
	}
	
	public String getCurrentView() {
		return content.getCaption().toString();
	}
	
	public void logout() {
		main.showLoginPanel();
	}

	@Override
	public void update(Observable arg0, Object scanversionObject) {
		Scanversion scanversion = (Scanversion) scanversionObject;
		homePanel.updateGUI();
		scanPanel.updateGUI(scanversion);
		archivePanel.updateGUI();
		statisticsPanel.updateGUI();		
	}
}
